package me.banxi.mia.account.email;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class FreemarkerHelper {

	public static String getHtmlText(String dir,String name){
			Configuration cfg = new Configuration();
			try {
				File file = new File(dir);
				cfg.setDirectoryForTemplateLoading(file);
				cfg.setObjectWrapper(new DefaultObjectWrapper());
				
				Template template = cfg.getTemplate(name);
				
				Map<String, List<String>> root = new HashMap<String, List<String>>();
				
				List<String> lrcList1 = getLrcList(file.getPath()+"/fukua_lrc1.txt");
				List<String> lrcList2 = getLrcList(file.getPath()+"/fukua_lrc2.txt");
				
				root.put("lrcList1", lrcList1);
				root.put("lrcList2", lrcList2);
				
				Writer out = new PrintWriter(new File(file.getPath()+"/fukua.htm"));
				
				template.process(root, out);
				
				out.flush();
				out.close();
				
				
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (TemplateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return null;
	}

	private static List<String> getLrcList(String name) throws FileNotFoundException {
	   List<String> lrc = new ArrayList<String>();
		Scanner input = new Scanner(new File(name));
		
		while(input.hasNext()){
			lrc.add(input.nextLine());
		}
		return lrc;
	}
}
